var PHOTOS_COUNT = 0;
var bigImages = {};
var thumbImages = {};
var hugeImages = {};
var THUMB_INDEX = 0;
var LEFT_IMAGE = 0;

function initGallery(imageID, sliderID) {
	//PHOTOS_COUNT = Object.keys(thumbImages).length;
	if (PHOTOS_COUNT == 0) {
		alert('TODO');
		return;
	}
	$('#phcount').html(PHOTOS_COUNT); 
	for (i in thumbImages) {
		var a = $('<a />').attr('href', 'javascript:;').attr('rel', i);
		a.click(function(){
			$('#' + imageID).attr('src', bigImages[$(this).attr('rel')].src);
			$('#' + sliderID + ' a:eq(' + THUMB_INDEX + ')').removeClass('active');
			THUMB_INDEX = $(this).index();
			$(this).addClass('active');
			$('#phnumber').html(THUMB_INDEX + 1);
		});
		a.append($('<img />').attr('src', thumbImages[i].src));
		$('#' + sliderID).append(a);
		$('#hugephotos').append($('<a href="' + hugeImages[i] + '"></a>'));
	}
	$('#hugephotos a').lightBox({
		imageLoading: HTTP_HOST + '/css/jquery/lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose: HTTP_HOST + '/css/jquery/lightbox/images/close_btn.gif',
		imageBtnPrev: HTTP_HOST + '/css/jquery/lightbox/images/left_arrow.png',
		imageBtnNext: HTTP_HOST + '/css/jquery/lightbox/images/right_arrow.png',
		imageBlank: HTTP_HOST + '/css/jquery/lightbox/images/lightbox-blank.gif',
		txtImage: _txt_image,
		txtOf: _txt_of
	});
	$('#showhugephotos').click(function(){
		$('#hugephotos a:eq(' + THUMB_INDEX + ')').trigger('click');
	});
	$('#' + imageID).attr('src', bigImages[$('#sliderlist a:eq(0)').attr('rel')].src);
	$('#' + imageID).load(function(){
		updateImageCover($('#' + imageID).get(0));
	});
	$('#' + sliderID + ' a:eq(0)').addClass('active');
	$('.slideright span').click(function(){
		if (THUMB_INDEX >= (PHOTOS_COUNT - 1)) return;
		
		if (THUMB_INDEX + 1 - LEFT_IMAGE > 4) {
			$('#' + sliderID + ' a:eq(' + LEFT_IMAGE + ')').hide();
			LEFT_IMAGE++;
		}
		$('#' + sliderID + ' a:eq(' + (THUMB_INDEX + 1) + ')').trigger('click');
	});
	$('.slideleft span').click(function(){
		if (THUMB_INDEX <= 0) return;
		
		if (THUMB_INDEX <= LEFT_IMAGE) {
			$('#' + sliderID + ' a:eq(' + (LEFT_IMAGE - 1) + ')').show();
			LEFT_IMAGE--;
		}
		$('#' + sliderID + ' a:eq(' + (THUMB_INDEX - 1) + ')').trigger('click');
	});
	$('.bphslider a.left').click(function(){
		$('.slideleft span').trigger('click');
	});
	$('.bphslider a.right').click(function(){
		$('.slideright span').trigger('click');
	});
}

function updateImageCover(el) {
	$('img:eq(1)', $(el).parent()).css('height', el.height + 2);
	$('.bphslider a').css('height', el.height + 2);
	//$('.bphslider a').css('margin-top', -el.height - 2);
}
